package com.efast.dmom.mgmt.service.mes.impl;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.util.date.DateUtils;
import com.efast.dmom.mgmt.dao.mes.PmPartModelAndVinRuleDao;
import com.efast.dmom.mgmt.dao.mes.PmVinYearDao;
import com.efast.dmom.mgmt.dao.mes.VinRuleDao;
import com.efast.dmom.mgmt.entity.mes.PmPartModelAndVinRule;
import com.efast.dmom.mgmt.entity.mes.VinRule;
import com.efast.dmom.mgmt.entity.mes.VinYear;
import com.efast.dmom.mgmt.service.mes.VinRuleService;
import com.ibm.icu.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VinRuleServiceImpl implements VinRuleService {
	@Autowired
	private PmPartModelAndVinRuleDao vinRuleDao;
	@Autowired
	private PmVinYearDao vinYearDao;
	@Autowired
	private VinRuleDao ruleDao;
	@Transactional(readOnly = false)

	@Override
    public List<String> returnVinNum(String partNoVersion,Integer num) {
		
		//获取Vin年份代码
		String vinYearValue;
		String vinPrefix;
		String assemblyPlant;
		int ProNumber;
	    PmPartModelAndVinRule vinRule;
		Date d = new Date();
		List<String> listVin = new ArrayList<>();
		SimpleDateFormat dt = new SimpleDateFormat("yyyy");
		String year = dt.format(d);
		List<VinYear> yearInfoList =vinYearDao.getVinYearInfo(year);
		if(yearInfoList!=null && yearInfoList.size()>0){
			vinYearValue=yearInfoList.get(0).getYear_value();
		}else {
			return listVin;
		} 
		List<PmPartModelAndVinRule>listInfo=vinRuleDao.getVinRuleInfo(partNoVersion); 
		
		if(listInfo.size()>0) {
		  vinPrefix=listInfo.get(0).getVinPrefix(); //vin号前8位
		  assemblyPlant= listInfo.get(0).getAssemblyPlant(); //装配厂 长度 1 String AssemblyPlant
		  }else {			  
			return listVin;
		 }
		//根据车型类别获取流水号信息
		List<PmPartModelAndVinRule> listProNumInfo = vinRuleDao.getProNumInfo(listInfo.get(0).getModelType());
		if(listProNumInfo==null || listProNumInfo.size()<1) {
			return listVin;
		}
		vinRule=listProNumInfo.get(0);
		if(listProNumInfo.get(0).getYear().equals(year)) {
			ProNumber=vinRule.getProNumber();			  
		}else {
			ProNumber=1;
			vinRule.setYear(year);
		}
		for(int i=0;i<num;i++) {
			StringBuffer bfBufferProNumber=new StringBuffer(String.valueOf(ProNumber));
	        bfBufferProNumber.reverse();
	        int size=6-bfBufferProNumber.length();
	        for(int j=0;j<size;j++) {
	        	bfBufferProNumber.append("0");
	        	}
		    bfBufferProNumber.reverse(); 
		    //获取校验位 
		    String checkNo=getCheckNo(vinPrefix,vinYearValue+assemblyPlant+bfBufferProNumber); 
		    //生成VIN号 
		    String vinNo=vinPrefix+checkNo+vinYearValue+assemblyPlant+bfBufferProNumber;
		    listVin.add(vinNo);
		    System.out.println(vinNo);
			ProNumber++;
		}

	    vinRule.setProNumber(ProNumber);
	    vinRule.setCompanyCode(SpringWebUtils.getRequestCompany());
	    vinRule.setLastUpdateUser(SpringWebUtils.getRequestUser());
	    vinRule.setLastUpdateTime(DateUtils.getCurrentDate());
		  
	    vinRuleDao.updateProNum(vinRule);
		return listVin;

    }

	@Override
	public String getCheckNo(String vinPrefix,String vis) {
		int sum = 0;
		int relevantValue;
		int weightedFactor;
		String checkNo;
		// 前8位之和
		for (int i = 0; i < vinPrefix.length(); i++) {
			relevantValue = getRelevantValue(vinPrefix.substring(i, i + 1));
			weightedFactor = getWeightedFactor(i + 1);
			sum += relevantValue * weightedFactor;
		}
		// 10-17位
		for (int i = 0; i < vis.length(); i++) {
			relevantValue = getRelevantValue(vis.substring(i, i + 1));
			weightedFactor = getWeightedFactor(10 + i);
			sum += relevantValue * weightedFactor;
		}
		System.out.println(sum);
		int num = sum % 11;
		if (num == 10) {
			checkNo = "X";
		} else {
			checkNo = String.valueOf(num);
		}
		return checkNo;
	}

	@Override
	public int getRelevantValue(String vinCode) {
		int relevantValue = 0;
		switch (vinCode) {
		case "0":
			relevantValue = 0;
			break;
		case "1":
			relevantValue = 1;
			break;
		case "2":
			relevantValue = 2;
			break;
		case "3":
			relevantValue = 3;
			break;
		case "4":
			relevantValue = 4;
			break;
		case "5":
			relevantValue = 5;
			break;
		case "6":
			relevantValue = 6;
			break;
		case "7":
			relevantValue = 7;
			break;
		case "8":
			relevantValue = 8;
			break;
		case "9":
			relevantValue = 9;
			break;
		case "A":
			relevantValue = 1;
			break;
		case "B":
			relevantValue = 2;
			break;
		case "C":
			relevantValue = 3;
			break;
		case "D":
			relevantValue = 4;
			break;
		case "E":
			relevantValue = 5;
			break;
		case "F":
			relevantValue = 6;
			break;
		case "G":
			relevantValue = 7;
			break;
		case "H":
			relevantValue = 8;
			break;
		case "J":
			relevantValue = 1;
			break;
		case "K":
			relevantValue = 2;
			break;
		case "L":
			relevantValue = 3;
			break;
		case "M":
			relevantValue = 4;
			break;
		case "N":
			relevantValue = 5;
			break;
		case "P":
			relevantValue = 7;
			break;
		case "R":
			relevantValue = 9;
			break;
		case "S":
			relevantValue = 2;
			break;
		case "T":
			relevantValue = 3;
			break;
		case "U":
			relevantValue = 4;
			break;
		case "V":
			relevantValue = 5;
			break;
		case "W":
			relevantValue = 6;
			break;
		case "X":
			relevantValue = 7;
			break;
		case "Y":
			relevantValue = 8;
			break;
		case "Z":
			relevantValue = 9;
			break;
		}
		return relevantValue;

	}

	@Override
	public int getWeightedFactor(Integer count) {
		int weightedFactor = 0;
		if (count == 1) {
			weightedFactor = 8;
		} else if (count == 2) {
			weightedFactor = 7;
		} else if (count == 3) {
			weightedFactor = 6;
		} else if (count == 4) {
			weightedFactor = 5;
		} else if (count == 5) {
			weightedFactor = 4;
		} else if (count == 6) {
			weightedFactor = 3;
		} else if (count == 7) {
			weightedFactor = 2;
		} else if (count == 8) {
			weightedFactor = 10;
		} else if (count == 9) {
			weightedFactor = 0;
		} else if (count == 10) {
			weightedFactor = 9;
		} else if (count == 11) {
			weightedFactor = 8;
		} else if (count == 12) {
			weightedFactor = 7;
		} else if (count == 13) {
			weightedFactor = 6;
		} else if (count == 14) {
			weightedFactor = 5;
		} else if (count == 15) {
			weightedFactor = 4;
		} else if (count == 16) {
			weightedFactor = 3;
		} else if (count == 17) {
			weightedFactor = 2;
		}
		return weightedFactor;

	}

	@Override
	public Boolean checkVin(String partNoVersion, String vin) {
		if(vin== null||vin.length()!=17) {
			return false;
		}
		String vinPrefix;
		String trimVin = vin.trim();
		String vinPrefix2=trimVin.substring(0, 8);
		String yearCode=trimVin.substring(9, 10);
		List<PmPartModelAndVinRule>listInfo=vinRuleDao.getVinRuleInfo(partNoVersion); 
		if(listInfo!=null && listInfo.size()>0 ) {
			vinPrefix=listInfo.get(0).getVinPrefix();
			if(!vinPrefix.equals(vinPrefix2)) {
				return false;
			}
		}
		Date d = new Date();
		SimpleDateFormat dt = new SimpleDateFormat("yyyy");
		String year = dt.format(d);
		String vinYearCode;
		List<VinYear> yearInfoList =vinYearDao.getVinYearInfo(year);
		if(yearInfoList!=null && yearInfoList.size()>0){
			vinYearCode=yearInfoList.get(0).getYear_value();
			if(!vinYearCode.equals(yearCode)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public ResultBean saveVinRuleInfo(VinRule enti) {
		ResultBean result = new ResultBean(true, "'");
		result.setSuccess(true);
		List<VinRule> listRuleInfo = ruleDao.getVinRuleInfo(enti.getPartNo());
		if(listRuleInfo!=null && listRuleInfo.size()>0) {
			result.setSuccess(false);
			result.setMessage("该车型编码已经存在Vin规则配置!");
			return result;
		}
		if("A".equals(enti.getModelType())) {
			if(enti.getVinPrefix()==null ||"".equals(enti.getVinPrefix())) {
				result.setSuccess(false);
				result.setMessage("货车的车间号前缀不能为空！!");
				return result;	
			}
			if(enti.getAssemblyPlant()==null ||"".equals(enti.getAssemblyPlant())) {
				result.setSuccess(false);
				result.setMessage("货车的装配厂不能为空！!");
				return result;	
			}
		}
		enti.setCreateTime(new Date());
		enti.setCreateUser(SpringWebUtils.getRequestUser());
		enti.setCompanyCode(SpringWebUtils.getRequestCompany());
		ruleDao.saveVinRuleInfo(enti);
		
		return result;
		
	}

	@Override
	public void deleteVinRuleInfo(Integer id) {
		ruleDao.deleteVinRuleInfo(id);
		ruleDao.deleteVehiclePartInfo(id);
	}

}
